package cn.caojun.auth.controller;

import cn.caojun.auth.annotation.caojunPermission;
import cn.caojun.auth.domain.Menu;
import cn.caojun.auth.query.MenuQuery;
import cn.caojun.auth.service.IMenuService;
import cn.caojun.basic.utils.AjaxResult;
import cn.caojun.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
@caojunPermission(name="菜单权限",desc = "菜单权限")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @PutMapping
    public AjaxResult addOrUpData(@RequestBody Menu menu){
        try {
            if (Objects.isNull(menu.getId())){
                menuService.add(menu);
            }else {
                menuService.update(menu);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("数据更新失败"+e.getMessage());
        }
    }

    @GetMapping
    public AjaxResult getAllList(){
        try{
            List<Menu> menuList=menuService.getAll();
            return AjaxResult.me().setObject(menuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单列表查找失败"+e.getMessage());
        }
    }

    @GetMapping("/tree")
    public AjaxResult getMenuTree(){
        try{
            List<Menu> menuList=menuService.getMenuTree();
            return AjaxResult.me().setObject(menuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单树查找失败"+e.getMessage());
        }
    }

    @GetMapping("/menuTree/{loginUserId}")
    public AjaxResult getTree(@PathVariable("loginUserId")Long loginUserId){
        try{
            List<Menu> menuList=menuService.getTree(loginUserId);
            return AjaxResult.me().setObject(menuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单树查找失败"+e.getMessage());
        }
    }

    @PostMapping
    public AjaxResult getlimitList(@RequestBody MenuQuery menuQuery){
        try{
            PageList<Menu> menuList=menuService.getLimteList(menuQuery);
            return AjaxResult.me().setObject(menuList);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单列表查找失败"+e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id")Long id){
        try{
            menuService.removeById(id);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单列表查找失败"+e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult removeById(@RequestBody List<Long> ids){
        try{
            menuService.batchRemove(ids);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("表单列表查找失败"+e.getMessage());
        }
    }
}
